package fzu.functiongather.Utils;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.ProgressBar;

import fzu.functiongather.R;

/**
 * Created by WLT on 2016/7/13.
 */
public class RoundProgressbar extends ProgressBar{

    private static final int DEFAULT_TEXT_COLOR=0xFFFC00D1;
    private static final int DEFAULT_COLOR_UNREACH=0xFFD3D6D7;
    private static final int DEFAULT_HIGHT_UNREACH=2;//dp
    private static final int DEFAULT_COLOR_REACH=DEFAULT_TEXT_COLOR;//sp
    private static final int DEFAULT_HIGHT_REACH=2;//dp

    private int mTextColor=DEFAULT_TEXT_COLOR;
    private int mUnReachColor=DEFAULT_COLOR_UNREACH;
    private int mUnReachWidth=dp2px(DEFAULT_HIGHT_UNREACH);//未完成进度环的宽度
    private int mReachColor=DEFAULT_COLOR_REACH;
    private int mReachWidth =dp2px(DEFAULT_HIGHT_REACH);//完成进度环的宽度

    private Paint mPaint=new Paint();//画笔
    private int  mRealWidth;//宽度

    private int mRadius =dp2px(30);//默认半径
    private int mMaxPaintWidth;//最大画笔宽度


    public RoundProgressbar(Context context) {
        this(context,null);
    }

    public RoundProgressbar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundProgressbar(Context context, AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);
        mReachWidth =(int)(mUnReachWidth *2.5f);//完成进度环的宽度为未完成的2.5，美观效果

        //获取自定义属性
        TypedArray ta=context.obtainStyledAttributes(attrs, R.styleable.RoundProgressbar);
        mRadius=(int)ta.getDimension(R.styleable.RoundProgressbar_radius,mRadius);
        ta.recycle();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setStrokeCap(Paint.Cap.ROUND);//连接处设置为弧形
    }

    /*
    *
    * */
    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mMaxPaintWidth=Math.max(mReachWidth, mUnReachWidth);
        //默认四个padding一致
        int expect=mRadius*2+mMaxPaintWidth+getPaddingLeft()+getPaddingRight();

        int width=resolveSize(expect,widthMeasureSpec);//测量宽度
        int height=resolveSize(expect,heightMeasureSpec);//测量高度

        int readWidth=Math.min(width,height);
        mRadius=(readWidth-getPaddingLeft()-getPaddingRight()-mMaxPaintWidth)/2;

        setMeasuredDimension(readWidth, readWidth);
    }

    /*
    * 画圆形进度
    * */
    @Override
    protected synchronized void onDraw(Canvas canvas) {
        String text=getProgress()+"%";
        float textWidth=mPaint.measureText(text);
        float textHeight=(mPaint.descent()+mPaint.ascent())/2;
        canvas.save();
        canvas.translate(getPaddingLeft() + mMaxPaintWidth / 2, getPaddingTop()+ mMaxPaintWidth / 2);
        mPaint.setStyle(Paint.Style.STROKE);
        //画未完成进度
        mPaint.setColor(mUnReachColor);
        mPaint.setStrokeWidth(mUnReachWidth);
        canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);
        //画已完成进度
        mPaint.setColor(mReachColor);
        mPaint.setStrokeWidth(mReachWidth);
        float sweepAngle=getProgress()*1.0f/getMax()*360;//弧度
        canvas.drawArc(new RectF(0,0,mRadius*2,mRadius*2), 0, sweepAngle, false, mPaint);//绘制弧
        //画text
        mPaint.setColor(mTextColor);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawText(text,mRadius-textWidth/2,mRadius-textHeight,mPaint);
        canvas.restore();

    }

    /*
           * dp到px的转化
           * */
    private  int dp2px(int dpVal)
    {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, getResources().getDisplayMetrics());
    }

    /*
    * sp到px的转化
    * */
    private int sp2px(int spVal)
    {
        return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,spVal,getResources().getDisplayMetrics());
    }
}
